package demo;

//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginValidation {
	User user;
	//private PreparedStatemen pstmt;
	
	public LoginValidation(User user) {
		this.user = user;
	}
	
	public boolean isValidate() {
		String pw = getTruePassword(user);
		return pw.equals(user.getPassword());
	}
	
	public String getTruePassword(User user) {		
		ResultSet result;
		try {
			result = getPassword(user);
			if(result.next()) {
				return result.getString(1);		// password should be the second column!!!
			} else
				return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;
	}
	
	/** 
     * Initialize database connection
     */
    private ResultSet getPassword(User user) {
    	DBConnector db = DBConnector.getDBConnector();
    	db.prepareStatement("select password from customer where customer.customer_ID = ?");
    	db.bindParameter(user.getUserId());
    	return db.queryPstmt();
    	/*
      try {
    	  // Declare driver and connection string
    	  String driver = "com.mysql.jdbc.Driver";
    	  String connectionString = "jdbc:mysql://localhost:3306/lumen?" 
    		  						+ "user=root&" 
    		  						+ "password=89djm0210";

				// Step1: Load the driver
				Class.forName( driver );
				System.out.println("step1 ok!");

				// Step2: Connect to the database
				Connection conn = DriverManager.getConnection( connectionString );
				System.out.println("step2 ok!");

				// Step3: Create a Prepared Statement
				pstmt = conn.prepareStatement( "select password from customer where customer.customer_ID = ?" );
				System.out.println("step3 ok!");
				
      } catch ( Exception ex ) {
    	  ex.printStackTrace();
      }
      */
    }
    
    /*
    private ResultSet bindUserIdAndExecute(User user) throws SQLException {
    	System.out.println(user.getUserId());
    	pstmt.setString(1, user.getUserId());
    	return pstmt.executeQuery();
    }
    */
	
}
